#!/bin/bash

ACTION=$1
shift

do_help() {
    echo "${1} scanlist" >&2
    echo "${1} list" >&2
}

remove_last_word() {
    echo "${@:1:$#-1}"
}

do_list() {
    connmanctl services | while read; do
        if [[ $REPLY == *'wifi_'* ]]; then
          WITHOUT_PREFIX=${REPLY:4}
          echo $(remove_last_word $WITHOUT_PREFIX)
        fi
    done
}

do_scanlist() {
    connmanctl scan wifi >/dev/null 2>/dev/null
    do_list
}

case "${ACTION}" in
    "list")
	do_list
	;;
    "scanlist")
	do_scanlist
	;;
    *)
	do_help "${0}"
esac
